%设置仿真时间
startTime = datetime(2024,1,7,14,5,0);
stopTime = startTime + hours(6);
sampleTime = 30; % seconds
%创建仿真场景
sc = satelliteScenario(startTime,stopTime,sampleTime);
%导入星历数据
tleFile = "leoSatelliteConstellation.tle";
%tleFile="lridium.tle";
%创建卫星，并设置点位和轨道颜色
sat = satellite(sc,tleFile);
set(sat(1:13), MarkerColor="#FF6929");
set(sat(14:26), MarkerColor="#139FFF");
set(sat(27:40), MarkerColor="#64D413");
orbit = [sat(:).Orbit];
set(orbit(1:13), LineColor="#FF6929");
set(orbit(14:26), LineColor="#139FFF");
set(orbit(27:40), LineColor="#64D413");
fprintf('卫星加载完毕\n')
%创建探测器
names = sat.Name + " Camera";
cam = conicalSensor(sat,"Name",names,"MaxViewAngle",90);
fprintf('探测器加载完毕\n')
%创建多个地面站并展示
gsUS = groundStation(sc, 42.30048, -71.34908, ...
    MinElevationAngle=10, Name="Natick");
gsUS.MarkerColor = "red";
gsDE = groundStation(sc, 48.23206, 11.68445, ...
    MinElevationAngle=10, Name="Munchen");
gsDE.MarkerColor = "red";
gsIN = groundStation(sc, 12.94448, 77.69256, ...
    MinElevationAngle=10, Name="Bangalore");
gsIN.MarkerColor = "red";

figure
geoscatter([gsUS.Latitude gsDE.Latitude gsIN.Latitude], ...
    [gsUS.Longitude gsDE.Longitude gsIN.Longitude], "red", "filled")
geolimits([-75 75], [-180 180])
title("Ground Stations")
fprintf('地面站加载完毕\n')

accessUS = access(gsUS, sat);
accessDE = access(gsDE, sat);
accessIN = access(gsIN, sat);
%匹配地面站和卫星
set(accessUS, LineWidth="1");
set(accessUS(1:13), LineColor="#FF6929");
set(accessUS(14:26), LineColor="#139FFF");
set(accessUS(27:40), LineColor="#64D413");

set(accessDE, LineWidth="1");
set(accessUS(1:13), LineColor="#FF6929");
set(accessUS(14:26), LineColor="#139FFF");
set(accessUS(27:40), LineColor="#64D413");

set(accessIN, LineWidth="1");
set(accessUS(1:13), LineColor="#FF6929");
set(accessUS(14:26), LineColor="#139FFF");
set(accessUS(27:40), LineColor="#64D413");
%查看接入报告
intervalsUS = accessIntervals(accessUS);
accessintervalsUS = sortrows(intervalsUS, "StartTime", "ascend");



%查看AER报告
timeVector = startTime:seconds(sampleTime):stopTime;
[azimuth,elevation,range] = aer(sat(4),gsUS);
aer_table = table(timeVector', azimuth', elevation', range', ...
    'VariableNames', {'Time', 'Azimuth', 'Elevation', 'Range'});
%可视化AER
plotAER(aer_table)

%创建展示场景
v = satelliteScenarioViewer(sc,"ShowDetails",false);
%sat(4).ShowLabel = true;
gsUS.ShowLabel = true;
gsUS.LabelFontSize = 11;
gsDE.ShowLabel = true;
gsDE.LabelFontSize = 11;
gsIN.ShowLabel = true;
gsIN.LabelFontSize = 11;
%show(sat(4));
show(sat.Orbit);
fprintf('场景设置完毕\n')
fov = fieldOfView(cam([cam.Name] == "Satellite 4 Camera"));
fprintf('卫星选择完毕\n')
play(sc);
%计算通信参数
%计算时延
[a,b]=cal_latency(sat(5),gsDE,30,2);
%计算多普勒频移
[fShift,time,dopplerInfo] = cal_doppler(sat(5),gsDE,5e9,2);
